package com.lbs.jiepiaisi.biz;

import android.content.Intent;
import android.util.Log;

import com.lbs.jiepiaisi.bean.LoginPhoneResultBean;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.WorkLoginParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 * 时间：2018/1/23 09:42
 * 作用：根据安装单位手机号登录
 */

public class LoginPhoneBiz {

    public void phoneBiz(final String phone) {
        FormBody formBody = new FormBody
                .Builder()
                .add("phoneNo", phone)//安装单位手机号
                //.add("installld", phone)//安装安装id
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_INSTALL_LOGIN, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                LoginPhoneResultBean loginPhoneResultBean = new LoginPhoneResultBean();
                try {
                    //返回结果
                    String jsonString = response.body().string();
                    Log.e("json",jsonString);
                    loginPhoneResultBean = WorkLoginParser.parser(jsonString);
                    loginPhoneResultBean.getResult().setPhoneNo(phone);
                    loginPhoneResultBean.setStatus(Constant.STATUS_OK);
                } catch (Exception e) {
                    loginPhoneResultBean.setStatus(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                } finally {
                    Intent intent = new Intent(Constant.ACCOUNT_INSTALLER_LOGIN);
                    intent.putExtra(Constant.KEY_DATA, loginPhoneResultBean);
                    App.instance.sendBroadcast(intent);
                }
            }

            @Override
            public void failed() {
                LoginPhoneResultBean loginPhoneResultBean = new LoginPhoneResultBean();
                loginPhoneResultBean.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(Constant.ACCOUNT_INSTALLER_LOGIN);
                intent.putExtra(Constant.KEY_DATA, loginPhoneResultBean);
                App.instance.sendBroadcast(intent);
            }

            @Override
            public void noNetwork() {
                LoginPhoneResultBean loginPhoneResultBean = new LoginPhoneResultBean();
                loginPhoneResultBean.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(Constant.ACCOUNT_INSTALLER_LOGIN);
                intent.putExtra(Constant.KEY_DATA, loginPhoneResultBean);
                App.instance.sendBroadcast(intent);
            }
        });
    }
}
